package com.business.exception;

import com.comm.exception.BusinessException;
import com.comm.result.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * @author mis_wu
 * @date 2022/6/21 20:51
 * @todo 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandle {

    @ExceptionHandler({Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult<Object> exceptionHandler(Exception e, HttpServletResponse resp){
        e.printStackTrace();
        log.error("GlobalExceptionHandle error:{}",e.getMessage());
        String msg = "系统异常,请联系管理员!!!";
        if (e instanceof BusinessException) {
            msg = e.getMessage();
        }
        ResponseResult<Object> responseResult = new ResponseResult<>();
        responseResult.setCode("500");
        responseResult.setMsg(msg);
        return responseResult;
    }

}
